package com.unione.heathrobot.utils;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {
    private static final String TAG = "HttpUtil";
    private static final String BASE_URL = "http://192.168.9.70:8080/mapEdit/api/rosService/enableTiming";
    private static HttpUtil utils;
    private final OkHttpClient client = new OkHttpClient
            .Builder()
            .connectTimeout(40, TimeUnit.SECONDS)
            .readTimeout(40, TimeUnit.SECONDS)
            .build();
    private final Handler mainHandler = new Handler(Looper.getMainLooper());
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");


    public void closeSocket() {
        client.dispatcher().executorService().shutdown();
    }

    public static HttpUtil getInstance() {
        if (utils == null) {
            synchronized (HttpUtil.class) {
                if (utils == null) {
                    utils = new HttpUtil();
                }
            }
        }
        return utils;
    }

    public void get(final String url, final HttpCallback callback) {
        //  new Thread(() -> {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                mainHandler.post(() -> callback.onFailure(e.toString()));
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final int code = response.networkResponse().code();
                if (code == 200) {
                    final String string = response.body().string();
                    mainHandler.post(() -> callback.onSuccess(string));
                } else if (code == 500) {
                    mainHandler.post(() -> callback.onFailure(code + ""));
                } else {
                    mainHandler.post(() -> callback.onFailure(code + ""));
                }
                call.cancel();
            }
        });
        //   }).start();
    }
    public void getIntern(Request request, final HttpCallback callback) {
        //  new Thread(() ->
        Call call = HttpUtil.getInstance().client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                mainHandler.post(() -> callback.onFailure(e.toString()));
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final int code = response.networkResponse().code();
                if (code == 200) {
                    final String string = response.body().string();
                    mainHandler.post(() -> callback.onSuccess(string));
                } else if (code == 500) {
                    mainHandler.post(() -> callback.onFailure(code + ""));
                } else {
                    mainHandler.post(() -> callback.onFailure(code + ""));
                }
                call.cancel();
            }
        });
        //   }).start();
    }

    public void put(final String url, final String json, final HttpCallback callback) {
        new Thread(() -> {
            RequestBody body = RequestBody.create(JSON, json);
            Request request = new Request.Builder()
                    .url(url)
                    .put(body)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mainHandler.post(() -> callback.onFailure(e.toString()));
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final int code = response.networkResponse().code();
                    Log.d(TAG, "asd " + response);
                    if (code == 200) {
                        final String string = response.body().string();
                        mainHandler.post(() -> {
                            callback.onSuccess(string);
                        });
                    } else if (code == 500) {
                        mainHandler.post(() -> callback.onFailure(code + ""));
                    } else {
                        mainHandler.post(() -> callback.onFailure(code + ""));
                    }
                    call.cancel();
                }
            });
        }).start();
    }
}